package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ycsj.common.adapter.VpCommonAdapter;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.view.vp.NoScrollViewPager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.teacher.fragment.TeacherClassAnalyFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.TeacherStudentAnalyFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 分析界面
 * Created by Administrator on 2018/7/1 0001.
 */

public class TeacherStudAnalyzeActivity extends BaseActivity {

    //返回
    @BindView(R.id.iv_back)
    ImageView ivBack;

    //标题
    @BindView(R.id.tv_title)
    TextView tvTitle;

    //学生
    @BindView(R.id.lv_student)
    TextView lvStudent;

    //班级
    @BindView(R.id.lv_class)
    TextView lvClass;

    //不可滚动ViewPager
    @BindView(R.id.vp)
    NoScrollViewPager vp;

    //填充ViewPager的页面
    private List<Fragment> mFragments;

    //ViewPager的适配器
    private VpCommonAdapter vpAdapter;

    //班级号
    private String classNum;

    //Tab页面选中的资源图片集合
    private int[] tabSelectIds = {R.drawable.teacher_student_select, R.drawable.teacher_class_select};

    //Tab页面未选中的资源图片集合
    private int[] tabUnselectIds = {R.drawable.teacher_student_normal, R.drawable.teacher_class_analy_normal};

    /**
     * 跳转到当前界面的方法
     *
     * @param context
     * @param classNum
     */
    public static void goActivity(Context context, String classNum) {
        Intent intent = new Intent(context, TeacherStudAnalyzeActivity.class);
        intent.putExtra("classnum", classNum);
        context.startActivity(intent);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_student_analyze;
    }

    @Override
    protected void init() {

        initStateBar();

        //获取班级号
        initInent();

        //初始化视图
        initState();

        //设置适配器
        initVp();
    }

    /**
     * 初始化视图
     */
    public void initState() {
        tvTitle.setText("考勤管理");
        vp.setPagerEnabled(false);
    }

    /**
     * 获取班级号
     */
    private void initInent() {
        classNum = getIntent().getStringExtra("classnum");
    }

    /**
     * 初始化VieWPager
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        mFragments.add(TeacherStudentAnalyFragment.newInstance(classNum));
        mFragments.add(TeacherClassAnalyFragment.newInstance(classNum));
        vpAdapter = new VpCommonAdapter(getSupportFragmentManager(), mFragments);
        vp.setOffscreenPageLimit(mFragments.size());
        vp.setAdapter(vpAdapter);
    }


    /**
     * 同步ViewPager页面和切换选中Tab的方法
     *
     * @param pos
     */
    private void syncVpState(int pos) {
        vp.setCurrentItem(pos);
        if (pos == 0) {
            lvStudent.setBackgroundResource(tabSelectIds[0]);
            lvClass.setBackgroundResource(tabUnselectIds[1]);
        } else if (pos == 1) {
            lvStudent.setBackgroundResource(tabUnselectIds[0]);
            lvClass.setBackgroundResource(tabSelectIds[1]);
        }
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.lv_student, R.id.lv_class})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.lv_student) {
            //学生
            syncVpState(0);
        } else if (id == R.id.lv_class) {
            //班级
            syncVpState(1);
        }
    }
}
